package view;

import database.Database;
import inventory.InventoryItem;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Model;
import model.UserPage;

/**
 * UserPageXML implements the XML based user dashboard page view handling of the
 * Lithub website, which displays all of a user's account and inventory
 * information.
 *
 * Primarily used for the Android side of the Lithub project.
 * 
 * @author Reynaldo Gonzales.
 * @version 1.0
 *
 */
class UserPageXML extends View {

    private Model model;
    private HttpServletRequest request;
    private HttpServletResponse response;

    /**
     * UserPageXML(...) is a reciever of user dashboard page view requests and also
     * the responder to such as well.
     *
     * @param a HttpServletRequest request which holds the HTTP request
     * information of the requesting user.
     *
     * @param b HttpServletResponse response to the requesting user.
     *
     */
    UserPageXML(HttpServletRequest req, HttpServletResponse res) {
        request = req;
        response = res;
    }

    /**
     * invoke(modem m) is a model invoker which casts the invokation of the
     * userpage model implementation to try provide the proper dashboard
     * information of the requesting user.
     *
     * On success, the user object gains of the inventory and other related user
     * account attributes, which can be parsed by an XML parser to display the
     * user's account and inventory information.
     *
     * @param a model which refers to the model that implements the userpage.
     *
     * @catch generic exception.
     */
    @Override
    public void invoke(Model m) {
        PrintWriter out = null;
        try {
            UserPage u = (UserPage) m;
            if (u.requiresRedirect) {
                String username = u.user.username;
                String password = u.user.password;
                response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302
                response.sendRedirect("index.jsp?model=UserPage&view=UserPageXML&action=ShowUserPage&mobile=yes&username=" + username + "&password=" + password);
            } else {
                out = response.getWriter();
                response.setContentType("text/xml");
                out.write("<userPage>");
                out.write("<username>" + u.user.username + "</username>");
                for (int i = 0; i < u.user.getInventoryArraySize(); ++i) {
                    InventoryItem item = u.user.getItem(i);
                    out.write("<book isbn=\"" + item.itemISBN + "\">");
                    out.write("<title>" + item.itemName + "</title>");
                    out.write("<author>" + item.itemAuthor + "</author>");
                    out.write("<summary>" + item.itemSummary + "</summary>");
                    out.write("<userID>" + item.userID + "</userID>");
                    out.write("<ownerID>" + item.ownerID + "</ownerID>");
                    out.write("</book>");
                }
                out.write("</userPage>");
            }
        } catch (Exception ex) {
            Logger.getLogger(FrontPageHTML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
